package com.team.panicgame.view.vudduu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;
import com.team.panicgame.GameConstants;
import com.team.panicgame.models.vudduu.VudduuPlayer;
import com.team.panicgame.models.vudduu.VudduuWorld;

public class VudduuRenderer {
	private OrthographicCamera camera;
	private ShapeRenderer shapeRenderer = new ShapeRenderer();
	private SpriteBatch spriteBatch = new SpriteBatch();
	private FPSLogger fpsLogger = new FPSLogger();
	private float width, height;
	private VudduuPlayer player;
	private VudduuWorld world;
	
	ShapeRenderer debugRenderer = new ShapeRenderer();
	
	public VudduuRenderer(VudduuPlayer player, VudduuWorld world){
		loadResources();
		this.player = player;
		this.world = world;
		
		camera = new OrthographicCamera(GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		camera.position.x = GameConstants.GAME_WIDTH / 2f;
		camera.position.y = GameConstants.GAME_HEIGHT / 2f;
		camera.update();
	}
	
	public void loadResources(){
		loadTextures();
		loadSounds();
	}
	
	private void loadTextures(){
	}
	private void loadSounds(){
	}
	
	public void resize(float width, float height){
		this.width = width;
		this.height = height;
	}
	
	public void render(){
		initCamera();
		drawDebugMatrix();
		drawPlayer();
	}
	
	public void initCamera(){
		shapeRenderer.setProjectionMatrix(camera.combined);
		spriteBatch.getProjectionMatrix().setToOrtho2D(0f, 0, GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		fpsLogger.log();
	}
	
	public void drawPlayer() {
		debugRenderer.setProjectionMatrix(camera.combined);
		debugRenderer.begin(ShapeType.Rectangle);
		debugRenderer.setColor(new Color(0, 1, 1, 1));
		Rectangle rect = player.getShape();
		float cx = rect.x - (float)world.xini*world.MAP_CELL_SIZE;
		float cy = rect.y - (float)world.yini*world.MAP_CELL_SIZE;
		System.out.println("x: " + cx);
		System.out.println("y: " + cy);
		debugRenderer.rect(cx, cy, rect.width, rect.height);
		debugRenderer.end();
	}
	
	public void drawDebugMatrix() {
		debugRenderer.setProjectionMatrix(camera.combined);
		debugRenderer.begin(ShapeType.Rectangle);
		float cz = world.MAP_CELL_SIZE;
		int[][] M = world.getActualMap(player.getPosition());
		
		for(int i=0; i < (int)M.length ;i++) {
			for(int j=0; j < (int)M[0].length ;j++) {
				if(M[i][j] == 0) {
					debugRenderer.setColor(new Color(0.2f, 0.2f, 0.2f, 1));
					Rectangle rect = new Rectangle(i*cz, j*cz, cz, cz);
					debugRenderer.rect(rect.x, rect.y, rect.width, rect.height);
				}
			}
		}
		
		for(int i=0; i < (int)M.length ;i++) {
			for(int j=0; j < (int)M[0].length ;j++) {
				if(M[i][j] == 1) {
					debugRenderer.setColor(new Color(0.5f, 0.5f, 0, 1));
					Rectangle rect = new Rectangle(i*cz, j*cz, cz, cz);
					debugRenderer.rect(rect.x, rect.y, rect.width, rect.height);
				}
			}
		}
		
		debugRenderer.end();
	}
	
}
